1
1
.
.
1
1
.
.
6
6
C
C
a
a
t
t
c
c
h
h
E
E
x
x
c
c
e
e
p
p
t
t
i
i
o
o
n
n
-
-
U
U
s
s
i
i
n
n
g
g
@
@
C
C
o
o
n
n
t
t
r
r
o
o
l
l
l
l
e
e
r
r
A
A
d
d
v
v
i
i
c
c
e
e
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to catch Validation Exceptions by creating @ControllerAdvice Class.
Such Class will catch Exceptions from all Controllers.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_validation_catchexception_eceptionhandler (add Spring Boot Starters from the table)
Edit File: pom.xml (add validation dependency)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside package controllers)
– Create Class: GlobalExceptionHandler.java (inside package controllers)
pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation </artifactId>
</dependency>
MyController.java
package com.ivoronline.springboot_validation_catchexception_controlleradvice.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
@Controller
@Validated
public class MyController {
@ResponseBody
@RequestMapping("/Hello")
public String hello(
@RequestParam @NotBlank String name,
@RequestParam @NotNull Integer age
) {
return "Hello " + name;
}
}
http://localhost:8080/Hello
Tomcat
hello()
MyController
GlobalException
Handler
GlobalExceptionHandler.java
package com.ivoronline.springboot_validation_catchexception_controlleradvice.controllers;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
@ControllerAdvice
public class GlobalExceptionHandler {
@ResponseBody
@ResponseStatus(HttpStatus.BAD_REQUEST)
@ExceptionHandler(MissingServletRequestParameterException.class)
public String handleIdentifiersNotMatchingException(MissingServletRequestParameterException exception) {
//GET EXCEPTION DETAILS
String parameterType = exception.getParameterType(); //String
String parameterName = exception.getParameterName(); //name
String message = exception.getMessage(); //Required String parameter 'name' is not present
//RETURN MESSAGE
return message;
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello (reports only first error)
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
</dependencies>